package com.timeset.user.controller;

import com.timeset.activity.entity.Orgnaization;
import com.timeset.activity.entity.Student;
import com.timeset.photo.entity.Photo;
import com.timeset.user.entity.User;
import com.timeset.user.service.UserServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserServiceImpl userService;

    @RequestMapping("/findOrganizationById")
    public Orgnaization findOrganizationById(@RequestParam("id") int id){
        Orgnaization orgnaization=userService.findOrganizationById(id);
        return orgnaization;
    }

    @RequestMapping("/findStudentById")
    public Student findStudentById(@RequestParam("id") int id){
        return userService.findStudentById(id);
    }

    @RequestMapping("/updateOrgNameById")
    public int updateOrgNameById(@RequestParam("id") int id,@RequestParam("name") String name){
        int i=userService.updateOrgNameById(id, name);
        if(i>0) {//修改成功
                int k = userService.updateOrgIdentityById(id, 0);
                System.out.println("修改状态：：：：：：" + k);
        }
        return i;
    }

    @RequestMapping("/updateOrgCommById")
    public int updateOrgCommById(@RequestParam("id") int id,@RequestParam("recommend") String recommend){
        return userService.updateOrgInformationById(id, recommend);
    }
    @RequestMapping("/updateOrgConnectById")
    public int updateOrgConnectById(@RequestParam("id") int id,@RequestParam("connect") String connect){
        return userService.updateOrgConnectById(id, connect);
    }
    //修改认证状态
    @RequestMapping("/updateIdentityById")
    public int updateIdentityById(@RequestParam("id") int id,@RequestParam("status") int status,@RequestParam("role")int role){
        int i = -3;
        if(role==0){//组织
            i=userService.updateOrgIdentityById(id, status);
        }else if(role==1){//学生
            i=userService.updateStuIdentityById(id, status);
        }
        System.out.println("点击事件返回的值"+i);
        return i;
    }
    //修改组织认证资料
    @RequestMapping("/updateOrgIdentityContentById")
    public int updateOrgIdentityContentById(@RequestParam("id") int id,@RequestParam("identityContent") String identityContent){

        int j=userService.updateOrgIdentityContentById(id, identityContent);
        if(j==1){
            //将认证状态设为0，重新认证
            //查询之前是否已经认证
            if (userService.findOrganizationById(id).getIdentity() != 0) {//已认证
                //修改为未认证
                int k=userService.updateOrgIdentityById(id,0);
                System.out.println("修改状态：：：：：：" + k);
            }

        }
        return j;
    }




    //修改学生姓名
    @RequestMapping("/updateStuNameById")
    public int updateStuNameById(@RequestParam("id") int id,@RequestParam("name") String name){
        int i=userService.updateStuNameById(id, name);
        if(i==1){//修改完成，则将认证状态改为0
            userService.updateStuIdentityById(id,0);
        }
        return i;
    }
    //修改学生昵称
    @RequestMapping("/updateStuNickNameById")
    public int updateStuNickNameById(@RequestParam("id") int id,@RequestParam("nickname")String nickname){
        return userService.updateStuNickNameById(id, nickname);
    }
    //修改学生学号
    @RequestMapping("/updateStuNumberById")
    public int updateStuNumberById(@RequestParam("id") int id,@RequestParam("number")String number){
        int sno=userService.updateStuNumberById(id, number);
        if(sno==1){//修改完成，则将认证状态改为0
            userService.updateStuIdentityById(id,0);
        }
        return sno;
    }
    //修改学生学院
    @RequestMapping("/updateStuCollegeById")
    public int updateStuCollegeById(@RequestParam("id") int id,@RequestParam("college")String college){
        int co= userService.updateStuCollegeById(id, college);
        if(co==1){//修改完成，则将认证状态改为0
            userService.updateStuIdentityById(id,0);
        }
        return co;
    }

    //修改学生个人介绍
    @RequestMapping("/updateStuRecommend")
    public int updateStuRecommend(@RequestParam("id") int id,@RequestParam("commend")String commend){
        return userService.updateStuRecommend(id, commend);
    }
    //修改学生认证状态
    @RequestMapping("/updateStuIdentityById")
    public int updateStuIdentityById(@RequestParam("id") int id,@RequestParam("status")int status){
        return userService.updateStuIdentityById(id, status);
    }








   /* @RequestMapping("/registry")
    public String insertUser(@RequestParam("phone") String phone,
                             @RequestParam(value = "password") String password,
                             @RequestParam(value = "userName",required = false) String userName,
                             @RequestParam(value = "file",required = false) MultipartFile file,
                             HttpServletRequest request) {
        System.out.println("插入用户");
        User user = new User();
        if(userService.findUserByPhone(phone)==null) {
            user.setPhone(phone);
            if (password != null && !password.equals("")) {
                user.setPassword(password);
            }
            if (userName != null && !userName.equals("")) {
                user.setUserName(userName);
            }
            if (file != null) {
                //上传服务器
                // 生成新的文件名
                String fileName = System.currentTimeMillis()+file.getOriginalFilename();
                // 保存路径
                String destFileName=request.getServletContext().getRealPath("")+"headImg"+ File.separator+fileName;
                // 执行保存操作
                File destFile = new File(destFileName);
                if (!destFile.getParentFile().exists()){
                    destFile.getParentFile().mkdir();
                }
                try {
                    file.transferTo(destFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                user.setHeadImg("headImg"+ File.separator+fileName);
            }
            int result = userService.insertUser(user);
            if (result != 0) {
                return "OK";
            }else{
                return "NO";
            }
        }else{
           return "";
        }
    }

    @RequestMapping("/delete")
    public int deleteUser(@RequestParam("phone") String phone) {
        System.out.println("删除用户");
        int result = userService.deleteUser(phone);
        if (result != 0) {
            return 0;
        }
        return -1;
    }

    @RequestMapping("/updateUserName")
    public int updateUserNameByPhone(@RequestParam("phone") String phone, @RequestParam("userName") String userName) {
        System.out.println("修改用户名");
        int result = userService.updateUserNameByPhone(phone, userName);
        if (result != 0) {
            return 0;
        }
        return -1;
    }

    @RequestMapping("/updateUserImg")
    public int updateUserImgByPhone(@RequestParam("phone") String phone
            ,  @RequestParam(value = "file",required = false) MultipartFile file
            ,HttpServletRequest request) {
        System.out.println("修改用户头像");
        if (file != null) {
            //上传服务器
            // 生成新的文件名
            String headImg = System.currentTimeMillis()+file.getOriginalFilename();
            // 保存路径
            String destFileName=request.getServletContext().getRealPath("")+"headImg"+ File.separator+headImg;
            System.out.println(destFileName);
            // 执行保存操作
            File destFile = new File(destFileName);
            if (!destFile.getParentFile().exists()){
                destFile.getParentFile().mkdir();
            }
            try {
                file.transferTo(destFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int result = userService.updateUserImgByPhone(phone,headImg);
            if (result != 0) {
                return 0;
            }
        }
        return -1;
    }

    @RequestMapping("updateUserPassword")
    public int updatePasswordByPhone(@RequestParam("phone") String phone,@RequestParam("password") String password){
        System.out.println("个人——修改用户密码");
        int result = userService.updateUserPasswordByPhone(phone, password);
        if (result != 0) {
            return 0;
        }
        return -1;
    }

    @RequestMapping("updateUserPhone")
    public int updateUserPhoneByUserId(@RequestParam("phone") String phone,@RequestParam("id") String id){
        System.out.println("个人——修改用户手机号");
        if (userService.findUserByPhone(phone) != null){
            return -2;
        }
        int result = userService.updateUserPhoneByUserId(phone, id);
        if (result != 0) {
            return 0;
        }
        return -1;
    }

    @RequestMapping("/forget")
    public String updateUserPasswordByPhone(@RequestParam("phone") String phone, @RequestParam("password") String password) {
        System.out.println("修改用户密码");
        User user = userService.findUserByPhone(phone);
        if(user==null){  //没有此用户返回null
            return "";
        }else{   //有此用户则进行修改
            int count = userService.updateUserPasswordByPhone(phone,password);
            return count!=0?"OK":"NO";
        }
    }

    @RequestMapping("/all")
    public List<User> findAll() {
        System.out.println("查询所有用户");
        return userService.findAllPhone();
    }

    @RequestMapping("/findByPhone")
    public User findUserByPhone(@RequestParam("phone") String phone) {
        System.out.println("查询用户");
        return userService.findUserByPhone(phone);
    }

    @RequestMapping("/login")
    public User login(@RequestParam("phone")String phone,
                      @RequestParam("password")String password){
        System.out.println("登录");
        User user = userService.findByPhoneAndPassword(phone,password);
       return user;
    }
*/
}
